from home.models import Article, ArticleCategory


def common_data(request):
    articleCategories = ArticleCategory.objects.all()
    # 接受用户点击的分类id
    cat_id = request.GET.get('cat_id', 1)
    # 根据分类id进行分类的查询
    try:
        select_articleCategory = ArticleCategory.objects.get(id=cat_id)
    except ArticleCategory.DoesNotExist:
        select_articleCategory = ArticleCategory.objects.get(id=1)
    # 重新查询文章信息，按照浏览量降序排序（热门标签）
    hot_tags = Article.objects.all().order_by('-total_views').distinct()[:9]
    # 最新文章
    new_arts = Article.objects.order_by('-create_time')[:3]
    return {
        'hot_tags': hot_tags,
        'new_arts': new_arts,
        'articleCategories':articleCategories,
        'selectArticleCategory':select_articleCategory,
    }